<?php
namespace app\index\controller;
use app\model\Msg as MsgModel;
use app\model\Token as TokenModel;
use think\Db;


class Msg extends Base
{   
    /**
     * @Author   CCH
     * @DateTime 2020-05-23T12:10:10+0800
     * @return   数据列表
     */
    public function index(){
        $admin_id = $this->login_user['admin_id'];
        $map_params = [
            ['key'=>'oform_id','type'=>'=']
        ];
        $map = getSearchParam($map_params);
        $map[] = ['from_aid|to_aid','=',$admin_id];

        $model = new MsgModel();
        $list = $model->field('oform_id,count(*) as total')->where($map)->group('oform_id')->select();
        foreach ($list as $k => $vo) {
            $list[$k]['oform_name'] = Db::name('oform')->where('id',$vo['oform_id'])->value('name');

            $list[$k]['noread_num'] = $model->where([
                ['oform_id','=',$vo['oform_id']],
                ['to_aid','=',$admin_id],
                ['is_read','=',0],
            ])->count();
            $list[$k]['msg'] = $model->field('oform_id,order_id',true)->where([
                ['oform_id','=',$vo['oform_id']],
                ['from_aid|to_aid','=',$admin_id],
            ])->order('id','desc')->find();
        }
        return apiResult('2000','获取成功',$list);
    }

    /**
     * 获取详情
     * @Author   cch
     * @DateTime 2020-05-26T17:22:45+0800
     * @return   [type]                   [description]
     */
    public function read($oform_id){
        $admin_id = $this->login_user['admin_id'];
        $model = new MsgModel();
        $map = [
            ['oform_id','=',$oform_id]
        ];
        $map[] = ['from_aid|to_aid','=',$admin_id];
        $list = $model->where($map)->select()->toarray();
        foreach ($list as $k => $vo) {
            if (!empty($vo['from_aid'])) {
                $list[$k]['from_admin'] = Db::name('admin')->field('headimage,truename')->where('id',$vo['from_aid'])->find();
            }else{
                $list[$k]['from_admin'] = ['headimage'=>'','truename'=>'系统'];
            }
        }
        if (!empty($list)) {
            $ids = array_column($list,'id');
            $model->where([
                ['id','in',$ids],
                ['to_aid','=',$admin_id],
                ['is_read','=',0]
            ])->update(['is_read'=>1]);
        }
        return apiResult('2000','获取成功',$list);
    }

    /**
     * 保存
     * @Author   cch
     * @DateTime 2020-05-26T17:22:32+0800
     * @return   [type]                   [description]
     */
    public function allRead(){
        $admin_id = $this->login_user['admin_id'];
        $model = new MsgModel();
        $list = $model->where([
            ['to_aid','=',$admin_id],
            ['is_read','=',0]
        ])->update(['is_read'=>1]);
        return apiResult('2000','操作成功');
    }

    /**
     * 保存
     * @Author   cch
     * @DateTime 2020-05-26T17:22:32+0800
     * @return   [type]                   [description]
     */
    public function save(){
        $admin_id = $this->login_user['admin_id'];
        $param = file_get_contents('php://input');
        $param = json_decode($param,true);

        $fields = [
            'must'=>['content','oform_id'],
            'nomust'=>[]
        ];
        $save_data = paramFilter($param,$fields);
        if (!empty($save_data['error_msg'])) {
            $this->error($save_data['error_msg']);
        }
        
        $save_data['from_aid'] = $admin_id;
        $save_data['type'] = 1;
        $save_data['ctime'] = time();

        $order_id = Db::name('oform')->where('id',$save_data['oform_id'])->value('order_id');
        $save_data['order_id'] = $order_id;
        $save_data['to_aid'] = Db::name('order')->where('id',$order_id)->value('c_aid');

        if ( !MsgModel::saveData($save_data) ) {
            return apiResult('5000','提交失败');
        }else{
            return apiResult('2000','提交成功');
        }
    }

   


}
